package com.camp.android.trip.n.track.trip;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.camp.android.trip.n.track.DatabaseManager;
import com.camp.android.trip.n.track.Item;
import com.camp.android.trip.n.track.ItemActivity;
import com.camp.android.trip.n.track.R;

public class ItineraryListAdapter extends BaseAdapter implements OnClickListener, OnRatingBarChangeListener {

	private ArrayList<Item> itemList = null;
	private LayoutInflater inflater = null;
	private Activity activity;
	private DatabaseManager dbmanager;
	
	public ItineraryListAdapter(LayoutInflater inflater, Activity activity) {
		
		// Add variables
		this.inflater = inflater;
		this.activity = activity;
		this.dbmanager = new DatabaseManager(activity);
		
		// Fill itemList
		itemList = new ArrayList<Item>();
		itemList = dbmanager.getItems( ((TripItineraryActivity)activity).trip_id );

		Log.i("TAG","ItineraryListAdapter Initialized for tripid = " +((TripItineraryActivity)activity).trip_id );
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return itemList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return itemList.get(position).id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// Get custom item view
		View itemView = inflater.inflate(R.layout.itinerary_item, parent, false);
		
		// Get item object
		Item item = (Item)getItem(position);
		
		
		
		// Fill custom view with object's properties
		((TextView) itemView.findViewById(R.id.item_name)).setText(item.name);
		((RatingBar) itemView.findViewById(R.id.item_rating)).setRating(item.rating);
		((CheckBox) itemView.findViewById(R.id.item_checkbox)).setChecked(item.done);
		((TextView) itemView.findViewById(R.id.item_id)).setText(item.id+"");
		((TextView) itemView.findViewById(R.id.item_position)).setText(position+"");
		
		Log.i("ItineraryListAdapter","adding item view with id = " + ((TextView) itemView.findViewById(R.id.item_id)).getText());
		Log.i("ItineraryListAdapter","item.checked = "+item.done+" ----- view.checked = " + ((CheckBox) itemView.findViewById(R.id.item_checkbox)).isChecked() );
		// Initialize date format and fill view
		SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd");
		((TextView) itemView.findViewById(R.id.item_date)).setText(dateFormat.format(item.start.getTime()));
		
		// Add onClickListeners for view
		itemView.findViewById(R.id.item_name).setOnClickListener(this);
		itemView.findViewById(R.id.item_checkbox).setOnClickListener(this);
		((RatingBar)itemView.findViewById(R.id.item_rating)).setOnRatingBarChangeListener(this);
		return itemView;
	}

	@Override
	public void onClick(View view) {
		Log.i("ItineraryListAdapter","Item Clicked " + view);
		 
		if(view.getId() == R.id.item_checkbox) {
			
			
			// Get item position
			int position = Integer.parseInt(((TextView)((View)view.getParent())
											.findViewById(R.id.item_position)).getText().toString());
			// Get item from list and update it's done field
			Item item = itemList.get(position);
			item.done = ((CheckBox) view).isChecked();
			
			Log.i("ItineraryListAdapter","Checkbox clicked: "+ item.done + " --- for item id " + item.id);
			// Update item in database
			dbmanager.updateItem(item);
			itemList = dbmanager.getItems( ((TripItineraryActivity)activity).trip_id );
			((TripItineraryActivity) activity).refreshList();
		}
		else {
			// Create intent to go to ItemActivity
			// And add item's id to extra for initialization
			Intent intent = new Intent(activity,ItemActivity.class);
			intent.putExtra("item_id", ""+ ((TextView)((View)view.getParent()).findViewById(R.id.item_id)).getText());
			activity.startActivity(intent);
		}
	}

	@Override
	public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
		Log.i("ItineraryListAdapter", "Rating changed to: "+ rating);
		// Get item position
		int position = Integer.parseInt(((TextView)((LinearLayout)ratingBar.getParent().getParent())
										.findViewById(R.id.item_position)).getText().toString());
		// Get item from list and update it's done field
		Item item = itemList.get(position);
		item.rating = rating;
		
		// Update item in database
		dbmanager.updateItem(item);
		itemList = dbmanager.getItems( ((TripItineraryActivity)activity).trip_id );
		((TripItineraryActivity) activity).refreshList();
	}

}
